﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace CheckLift.Common.ViewConfigImpl
{
    public class GridViewElement : ConfigurationElement
    {
        /// <summary>
        /// Returns the key value.
        /// </summary>
        [ConfigurationProperty("ViewObjectName", IsRequired = true)]
        public string ViewObjectName
        {
            get
            {
                return this["ViewObjectName"] as string;
            }
        }

        [ConfigurationProperty("ColumnViews", IsDefaultCollection = false, IsRequired = false)]
        public ColumnViewCollection ColumnViews
        {
            get
            {
                ColumnViewCollection columnViews = (ColumnViewCollection)base["ColumnViews"];
                return columnViews;
            }
        }
        
    }

    public class GridViewCollection : ConfigurationElementCollection
    {
        public GridViewCollection()
        {

        }

        public GridViewElement this[int index]
        {
            get
            {
                return base.BaseGet(index) as GridViewElement;
            }
            set
            {
                if (base.BaseGet(index) != null)
                {
                    base.BaseRemoveAt(index);
                }
                this.BaseAdd(index, value);
            }
        }

        public GridViewElement this[object key]
        {
            get
            {
                return base.BaseGet(key) as GridViewElement;
            }
            set
            {
                if (base.BaseGet(key) == null)
                    this.BaseAdd(value);
            }
        }

        protected override ConfigurationElement CreateNewElement()
        {
            return new GridViewElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((GridViewElement)element).ViewObjectName;
        }

        public void Clear()
        {
            BaseClear();
        }

    }

    public class ViewConfigurationSection : ConfigurationSection
    {        
        // Declare a collection element represented 
        // in the configuration file by the sub-section
        // <BoolDescrCompany> <add .../> </BoolDescrCompany>         
        // instructs the .NET Framework to build a nested 
        // section like <boolDescrCompany> ...</boolDescrCompany>.
        [ConfigurationProperty("GridViewConfigurations", IsDefaultCollection = false)]
        public GridViewCollection GridViewConfigurations
        {
            get
            {
                GridViewCollection gridViewConfigurations = (GridViewCollection)base["GridViewConfigurations"];
                return gridViewConfigurations;
            }
        }


        protected override void DeserializeSection(
            System.Xml.XmlReader reader)
        {
            base.DeserializeSection(reader);
            // You can add custom processing code here.
        }

        protected override string SerializeSection(ConfigurationElement parentElement, string name, ConfigurationSaveMode saveMode)
        {
            string s = base.SerializeSection(parentElement, name, saveMode);
            // You can add custom processing code here.
            return s;
        }
    }


    public class ColumnViewElement : ConfigurationElement
    {
        /// <summary>
        /// Returns the key value.
        /// </summary>
        [ConfigurationProperty("BindToField", IsRequired = true)]
        public string BindToField
        {
            get
            {
                return this["BindToField"] as string;
            }
        }

        [ConfigurationProperty("TranslateKey", IsRequired = false)]
        public string TranslateKey
        {
            get
            {
                return this["TranslateKey"] as string;
            }
        }


        [ConfigurationProperty("IsVisible", IsRequired = true)]
        public bool IsVisible
        {
            get
            {
                return (bool)this["IsVisible"] ;                
            }
        }

        [ConfigurationProperty("IsFilterField", IsRequired = false)]
        public bool IsFilterField
        {
            get
            {
                return (bool)this["IsFilterField"];
                //return res == null ? false : bool.Parse(res);
            }
        }


        [ConfigurationProperty("FilterOrder", IsRequired = false)]
        public int FilterOrder
        {
            get
            {
                return (int)this["FilterOrder"];
               // return res == null ? -1 : int.Parse(res);                
            }
        }

        [ConfigurationProperty("ColumnType", IsRequired = false)]
        public string ColumnType
        {
            get
            {
                return this["ColumnType"] as string;
            }
        }
    }

    public class ColumnViewCollection : ConfigurationElementCollection
    {
        public ColumnViewCollection()
        {

        }

        public ColumnViewElement this[int index]
        {
            get
            {
                return base.BaseGet(index) as ColumnViewElement;
            }
            set
            {
                if (base.BaseGet(index) != null)
                {
                    base.BaseRemoveAt(index);
                }
                this.BaseAdd(index, value);
            }
        }

        protected override ConfigurationElement CreateNewElement()
        {
            return new ColumnViewElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((ColumnViewElement)element).BindToField;
        }

        public void Clear()
        {
            BaseClear();
        }

    }
}
